<template>
  <div class="app-container">
    <div class="top">
      <div class="operate">
        <el-button
          icon="el-icon-edit"
          size="mini"
          :disabled="single != true"
          @click="handleUpdate()"
          v-hasPermi="['prodLineMgmt:erpProductionLine:edit']"
          >编辑
        </el-button>
      </div>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item prop="factoryDeptCode" style="margin-right: 0">
          <organizationCascader
            :clearData="{
              factory: true,
              workshop: true,
            }"
            :arrData="queryParams"
            :showData="{
              factory: true,
              workshop: true,
              line: false,
              workSection: false,
            }"
            :isAllList="true"
            @change="queryParamsChange"
          ></organizationCascader>
        </el-form-item>
        <el-form-item prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入关键字"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="Table"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%"
      height="calc(100% - 44px - 50px)"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="产线编码" prop="erpProductionLineCode">
        <template slot-scope="{ row }">
          <el-button type="text" @click.stop="handleShowDetail(row)">{{
            row.erpProductionLineCode
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="产线名称" prop="erpProductionLineName">
      </el-table-column>
      <el-table-column label="所属工厂">
        <template slot-scope="{ row }">
          {{ row.bizDeptVo ? row.bizDeptVo.deptName : "" }}
        </template>
      </el-table-column>
      <el-table-column label="所属车间">
        <template slot-scope="{ row }">
          {{ row.workshopDto ? row.workshopDto.workshopName : "" }}
        </template>
      </el-table-column>
      <el-table-column label="产线类型" prop="lineType">
        <template slot-scope="{ row }">
          {{ produceLineTypeDict[row.lineType] || "" }}
        </template>
      </el-table-column>
      <el-table-column label="产线品类" prop="lineCategory">
        <template slot-scope="{ row }">
          {{
            lineCategoryDict[row.lineType]
              ? lineCategoryDict[row.lineType][row.lineCategory]
              : row.lineCategory
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="{ row }">
          <el-button
            v-hasPermi="['prodLineMgmt:erpProductionLine:edit']"
            type="text"
            @click.stop="handleUpdate(row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="pager.total"
      :page.sync="pager.pageNum"
      :limit.sync="pager.pageSize"
      @pagination="getList"
    />
    <DetailDialog
      :visible.sync="detailDialog.visible"
      :row="detailDialog.row"
      :disabled="detailDialog.disabled"
      @submit-success="getList"
    ></DetailDialog>
  </div>
</template>
<script>
import { listErpProductionLine } from "@/api/prodLineMgmt/productionLine";
import { getDicts } from "@/api/system/crccode";
import organizationCascader from "@/views/components/organizationCascader";
import DetailDialog from "./detailDialog";
export default {
  name: "ProductionLine",
  components: { DetailDialog, organizationCascader },
  data() {
    return {
      queryParams: {
        searchValue: null,
        factoryDeptCode: null,
        workshopCode: null,
      },
      list: [],
      single: null,
      loading: false,
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      detailDialog: {
        visible: false,
        row: null,
        disabled: false,
      },
      produceLineTypeDictList: [],
      produceLineTypeDict: {},
      lineCategoryDict: {},
    };
  },
  created() {
    // 默认获取第一个工厂-车间
    const results = this.getOwnFirstLine();
    this.queryParams.factoryDeptCode = results[0]?.factoryDeptCode || "";
    this.queryParams.workshopCode = results[1]?.workshopCode || "";
    //获取列表数据
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.single = null;
      const { pageNum, pageSize } = this.pager;
      const { factoryDeptCode, workshopCode, searchValue } = this.queryParams;
      const params = {
        query: {
          factoryDeptCode,
          workshopCode,
          searchValue,
        },
        pageNum,
        pageSize,
      };
      listErpProductionLine(params)
        .then((response) => {
          if (response.code == 200) {
            this.list = response.rows || [];
            this.pager.total = response.total;

            // 收集数据字典
            this.collectDict();
          } else {
            this.list = [];
            this.pager.total = 0;
          }
          this.loading = false;
        })
        .catch(() => {
          this.list = [];
          this.pager.total = 0;
          this.loading = false;
        });
    },
    async collectDict() {
      // 产线类型字典
      if (!this.produceLineTypeDictList?.length) {
        const response1 = await getDicts("produceLineType");
        this.produceLineTypeDictList = [...response1.data.produceLineType];
        response1.data.produceLineType.forEach(({ codevalue, codename }) => {
          this.$set(this.produceLineTypeDict, codevalue, codename);
        });
      }

      // 产线品类字典
      const allType = this.list.map((ele) => ele.lineType);
      this.produceLineTypeDictList.forEach(async (ele) => {
        if (
          ele.prop1 &&
          allType.includes(ele.codevalue) &&
          !this.lineCategoryDict[ele.codevalue]
        ) {
          const response2 = await getDicts(ele.prop1);
          if (!this.lineCategoryDict[ele.codevalue]) {
            this.$set(this.lineCategoryDict, ele.codevalue, {});
          }
          response2.data[ele.prop1].forEach(({ codevalue, codename }) => {
            // this.$set(this.lineCategoryDict[ele.codevalue], codevalue, codename)
            this.lineCategoryDict[ele.codevalue][codevalue] = codename;
          });
        }
      });
    },
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        this.single = false;
      } else if (selection.length == 1) {
        this.single = true;
      } else {
        this.single = null;
      }
    },
    queryParamsChange({ factoryDeptCode, workshopCode }) {
      this.queryParams.factoryDeptCode = factoryDeptCode;
      this.queryParams.workshopCode = workshopCode;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        searchValue: null,
        factoryDeptCode: null,
        workshopCode: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!row) {
        row = this.$refs.Table.selection[0] || null;
      }
      this.detailDialog.row = row;
      this.detailDialog.visible = true;
      this.detailDialog.disabled = false;
    },
    /** 详情按钮操作 */
    handleShowDetail(row) {
      this.detailDialog.row = row;
      this.detailDialog.visible = true;
      this.detailDialog.disabled = true;
    },
    /**
    handleExport() {

    }
    **/
  },
};
</script>
<style lang="scss" scoped>
.top {
  height: 44px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.pagination-container {
  margin: 0;
}
</style>
